package com.fengplus.demo.controller;

import com.fengplus.demo.entity.CommonResult;
import com.fengplus.demo.entity.AppUser;
import com.fengplus.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public CommonResult detail(@PathVariable Long id) {
        AppUser user = userService.findOne(id);
        if(user != null){
            user.setPassword(null);
            return new CommonResult<>(200, "查询成功", user);
        }else{
            return new CommonResult<>(400, "信息不存在");
        }
    }

    @PostMapping
    public CommonResult create(AppUser user) {
        int isSuccess = userService.save(user);
        if(isSuccess == 1){
            return new CommonResult<>(200, "保存成功");
        }else{
            return new CommonResult<>(400, "保存失败");
        }
    }

}
